import request  from "@/utils/request";
import store from "../store/modules/user"
// const pass = "admin123"
/*
  get  
  url   /user
  params:{username,password}
  */

export  async  function login(params){
  console.log(params,"login")
    return await request({
        url:"/user",
        params
    })
}


/*
  添加用户  
  url /user
  params:{username,nickname,type,password}
  // 
*/


export async function addUser(params){
    return await request({
      url:"/user",
      method:"post",
      data:params
    })
}

/*
  添加多个
  params:{addList:[]}
*/ 

export async function addUserList(params){
  // 程序没有问题的时候 为true [{usernmae,nickname,type},{usernmae,nickname,type}]
   let isOk = true
   // 创建一个数组id
   let data =[]
   //
   for(var item of params){
     try{
      let res =  await addUser(item)
      // res
      data.push(res.data.id)
     }catch(err){
      console.log(err)
      isOk =false
      return err
     }
   }
   if(isOk){
     return {
      code:200,
      msg:"添加成功",
      data
     }
   }else{
     return {
       code:201,
       msg:"添加失败",
       data
     }
   }
}

/*
  查重
  params:{username}
*/
export async function checkUser(params){
  return await request({
    url:"/user",
    params:params
})
}


// 修改个人资料 

export async function editor(id,params={}){
   return await request({
     url:"/user/"+id,
     method:"patch",
     data:params
   })
}

// 获取用户信息
export  async  function getUserInfo(id){
    return await request({
        url:"/user/"+id,
    })
}

//获取所有用户
/*
  当前页书
  每页条目数
  总条目
*/ 
export  async  function getUserAll(){
    return await request({
        url:"/user", 
    })
}


//获取用户列表
export async function getUserList(_page=1,_limit=5){
  //总条目数
  let {data} = await  getUserAll()
  let total = data.filter(item=>item.type!=0).length

  let res =  await request({
    url:"/user", 
    params:{
      _page,
      _limit,
      type:1
    }
  })
 
  return {
    ...res,
    total
  }
}

//删除用户

export async function delUser(id){
  return await request({
     url:"/user/"+id,
     method:"delete"
  })
}


//重置密码
export async function resetPass(id){
  return await request({
     url:"/user/"+id,
     method:"patch",
     data:{
       password:"admin123"
     }
  })
}





// 玩偶
// let obj = {name:"zhangsan",password:'admin123'};

// obj['age']="20"

// let obj1 = {...obj,age:20}   // {name:"zhangsan",password:'admin123'}



